package com.xrf;

import net.java.localization.LocalizedMessages;
import net.java.requestcycle.ThreadLocalData;
import net.java.tool.Utils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class GetResponse {
    private Document document;
    private Element errorElement;
    private Element foldersElement;
    private Element filesElement;

    public GetResponse(int number, String message) {
        // 初始化文档和错误处理
    }
    
    public GetResponse(int number) {
        this(number, null);
    }
    
    public void setError(int number, String message) {
        if (this.errorElement == null) {
            this.errorElement = this.document.createElement("Error");
            this.document.getDocumentElement().appendChild(this.errorElement);
        }
        this.errorElement.setAttribute("number", String.valueOf(number));
        if (Utils.isNotEmpty(message))
            this.errorElement.setAttribute("text", message);
    }
    
    public void setError(int number) {
        setError(number, null);
    }
    
    public void setFolders(List<String> folders) {
        if (this.foldersElement != null) {
            Element parent = (Element) this.foldersElement.getParentNode();
            parent.removeChild(this.foldersElement);
        }
        this.foldersElement = this.document.createElement("Folders");
        this.document.getDocumentElement().appendChild(this.foldersElement);
        for (Iterator i$ = folders.iterator(); i$.hasNext(); ) {
            String folder = (String) i$.next();
            Element folderElement = this.document.createElement("Folder");
            folderElement.setAttribute("name", folder);
            this.foldersElement.appendChild(folderElement);
        }
    }
    
    public void setFiles(List<Map<String, Object>> files) {
        if (this.filesElement != null) {
            Element parent = (Element) this.filesElement.getParentNode();
            parent.removeChild(this.filesElement);
        }
        this.filesElement = this.document.createElement("Files");
        this.document.getDocumentElement().appendChild(this.filesElement);
        long length = 1L;
        for (Iterator i$ = files.iterator(); i$.hasNext(); ) {
            Map file = (Map) i$.next();
            Element fileElement = this.document.createElement("File");
            fileElement.setAttribute("name", (String) file.get("name"));
            long tempLength = ((Number) file.get("size")).longValue();
            if (tempLength > 1024L)
                length = tempLength / 1024L;
            fileElement.setAttribute("size", String.valueOf(length));
            this.filesElement.appendChild(fileElement);
        }
    }
    
    public String toString() {
        StringWriter sw = new StringWriter();
        try {
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            DOMSource source = new DOMSource(this.document);
            StreamResult result = new StreamResult(sw);
            transformer.transform(source, result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return sw.toString();
    }
    
    public static GetResponse getOK() {
        return new GetResponse(0);
    }
    
    public static GetResponse getInvalidCommandError() {
        LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData.getRequest());
        return new GetResponse(1, lm.getInvalidCommandSpecified());
    }
    
    public static GetResponse getInvalidResourceTypeError() {
        LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData.getRequest());
        return new GetResponse(1, lm.getInvalidResouceTypeSpecified());
    }
    
    public static GetResponse getInvalidCurrentFolderError() {
        LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData.getRequest());
        return new GetResponse(1, lm.getInvalidCurrentFolderSpecified());
    }
    
    public static GetResponse getGetResourcesDisabledError() {
        LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData.getRequest());
        return new GetResponse(1, lm.getGetResourcesDisabled());
    }
    
    public static GetResponse getGetResourcesReadError() {
        LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData.getRequest());
        return new GetResponse(1, lm.getGetResourcesReadError());
    }
    
    public static GetResponse getCreateFolderDisabledError() {
        LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData.getRequest());
        return new GetResponse(1, lm.getCreateFolderDisabled());
    }
    
    public static GetResponse getInvalidNewFolderNameError() {
        LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData.getRequest());
        return new GetResponse(102, lm.getInvalidNewFolderNameSpecified());
    }
    
    public static GetResponse getFolderAlreadyExistsError() {
        LocalizedMessages lm = LocalizedMessages.getInstance(ThreadLocalData.getRequest());
        return new GetResponse(101, lm.getFolderAlreadyExistsError());
    }
}